package com.yizisu.basemvvm.mvvm.mvvm_helper

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.*
import com.yizisu.basemvvm.utils.safeGet

private fun createViewModel() {

}

/**
 * 创建viewModel
 */
inline fun <reified T : ViewModel> ViewModelStoreOwner.getViewModel(): T {
    return ViewModelProvider(this).get(T::class.java)
}

/**
 * activity的viewModel
 */
inline fun <reified T : ViewModel> Fragment.getActivityViewModel(): T {
    return activity?.getViewModel() ?: getViewModel()
}

/**
 * 父fragment的viewModel
 */
inline fun <reified T : ViewModel> Fragment.getParentFragmentViewModel(): T {
    return parentFragment?.getViewModel() ?: getViewModel()
}

/**
 * 注册livedata，忽略空的情况
 */
inline fun <T> LiveBean<T>.registerLiveBean(lifecycle: LifecycleOwner, crossinline observer: (LiveBean<T>) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        observer.invoke(this)
    }
    this.observe(lifecycle, observerObj)
    return observerObj
}

/**
 * 注册livedata，只处理成功的情况
 */
inline fun <T> LiveBean<T>.registerOnSuccessLiveBean(lifecycle: LifecycleOwner, crossinline success: (T) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        if (it.status == LiveBeanStatus.SUCCESS) {
            this@registerOnSuccessLiveBean.data?.let { data ->
                success.invoke(data)
            }
        }
    }
    this.observe(lifecycle, observerObj)
    return observerObj
}

/**
 * 注册livedata，忽略空的情况
 */
inline fun <T> MediatorLiveBean<T>.registerLiveBean(lifecycle: LifecycleOwner, crossinline observer: (MediatorLiveBean<T>) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        observer.invoke(this)
    }
    this.observe(lifecycle, observerObj)
    return observerObj
}

/**
 * 注册livedata，只处理成功的情况
 */
inline fun <T> MediatorLiveBean<T>.registerOnSuccessLiveBean(lifecycle: LifecycleOwner, crossinline success: (T) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        if (it.status == LiveBeanStatus.SUCCESS) {
            this@registerOnSuccessLiveBean.data?.let { data ->
                success.invoke(data)
            }
        }
    }
    this.observe(lifecycle, observerObj)
    return observerObj
}

/**
 * 注册livedata，忽略空的情况
 */
inline fun <T> LifecycleOwner.registerLiveBean(bean: LiveBean<T>, crossinline observer: (LiveBean<T>) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        observer.invoke(bean)
    }
    bean.observe(this, observerObj)
    return observerObj
}

/**
 * 注册livedata，只处理成功的情况
 */
inline fun <T> LifecycleOwner.registerOnSuccessLiveBean(bean: LiveBean<T>, crossinline success: (T) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        if (it.status == LiveBeanStatus.SUCCESS) {
            bean.data?.let { data ->
                success.invoke(data)
            }
        }
    }
    bean.observe(this, observerObj)
    return observerObj
}

/**
 * 注册livedata，忽略空的情况
 */
inline fun <T> LifecycleOwner.registerLiveBean(bean: MediatorLiveBean<T>, crossinline observer: (MediatorLiveBean<T>) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        observer.invoke(bean)
    }
    bean.observe(this, observerObj)
    return observerObj
}

/**
 * 注册livedata，只处理成功的情况
 */
inline fun <T> LifecycleOwner.registerOnSuccessLiveBean(bean: MediatorLiveBean<T>, crossinline success: (T) -> Unit): Observer<LiveBeanValue<T>> {
    val observerObj = Observer<LiveBeanValue<T>> {
        if (it.status == LiveBeanStatus.SUCCESS) {
            bean.data?.let { data ->
                success.invoke(data)
            }
        }
    }
    bean.observe(this, observerObj)
    return observerObj
}


